package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.mongo.SoulReport;
import com.tanhua.domain.vo.SimilarYouVo;
import com.tanhua.dubbo.api.mongo.SoulReportApi;
import com.tanhua.dubbo.utils.IdService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class SoulReportApiImpl implements SoulReportApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IdService idService;

    @Override
    public Long save(SoulReport soulReport) {
        soulReport.setReportId(idService.getNextId("soul_report"));
        mongoTemplate.save(soulReport);

        return soulReport.getReportId();

    }

    /**
     * 根据reportId 去查询结果对象
     * @param reportId
     * @return
     */
    @Override
    public SoulReport findByReportId(String reportId) {

        Query query = Query.query(Criteria.where("reportId").is(Long.valueOf(reportId)));
        SoulReport soulReport = mongoTemplate.findOne(query, SoulReport.class);
        return soulReport;
    }

    //查找与你相似
    @Override
    public  List<Long> findSimilarYou(Long userId, Integer resultId) {

        Query query = Query.query(Criteria.where("resultId").is(resultId));
        List<SoulReport> soulReports = mongoTemplate.find(query, SoulReport.class);

        List<Long> userIdLIst = new ArrayList<>();

        for (SoulReport soulReport : soulReports) {
            Long uid = soulReport.getUserId();
            if (uid != userId) {
                userIdLIst.add(uid);
            }
        }

        return userIdLIst;
    }
}
